import pymysql

def export_numbers_to_txt():
    mysql_host = '127.0.0.1'    #mysql 地址，本地就是localhost
    mysql_user = 'root'               #mysql 用户
    mysql_password = 'password'       #mysql 密码
    database = 'ry-vue'                 #mysql 数据库名
    connection = pymysql.connect(host=mysql_host,
                         user=mysql_user,
                         password=mysql_password,
                         database=database)
    # 连接到数据库
    try:
        with connection.cursor() as cursor:
            # 执行查询
            sql = "SELECT `number` FROM `user_number` WHERE `status` = 1  and update_time > '2024-09-24 19:10:50';"
            cursor.execute(sql)

            # 获取所有结果s
            results = cursor.fetchall()

            # 将结果写入到TXT文件
            with open('user_number.txt', 'w') as file:
                for row in results:
                    file.write(row[0] + '\n')  # 每个号码后面加一个换行符s
    finally:
        connection.close()

    print("导出完成，文件名：output.txt")

if __name__ == "__main__":
    export_numbers_to_txt()
